package com.atguigu.educms.service.impl;

import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.educms.entity.QaAnswer;
import com.atguigu.educms.entity.QaSecondAnswer;
import com.atguigu.educms.mapper.QaAnswerMapper;
import com.atguigu.educms.service.QaAnswerService;
import com.atguigu.educms.service.QaSecondAnswerService;
import com.atguigu.educms.util.PageFrontUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 问题--回帖详情表 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-03-14
 */
@Service
public class QaAnswerServiceImpl extends ServiceImpl<QaAnswerMapper, QaAnswer> implements QaAnswerService {

    @Autowired
    private QaSecondAnswerService secondAnswerService;

    @Override
    public FrontPageBean getAnswerAnd2Answer(Integer pageNo, Integer pageSize, Integer questionId) {
        Page<QaAnswer> pageParam = new Page<>(pageNo,pageSize);
        pageParam = selectAnswerByQuestionId(questionId, pageParam);
        for (QaAnswer record : pageParam.getRecords()) {
            List<QaSecondAnswer> answer2List = secondAnswerService.get2AnswerList(record.getId());
            record.setAnswer2List(answer2List);
        }
        return PageFrontUtils.copyPropertiesFrontPage(pageParam);
    }

    /**
     * 根据问题id获取回答列表
     * @param questionId
     * @param pageParam
     * @return
     */
    private Page selectAnswerByQuestionId(Integer questionId, Page pageParam) {
        QueryWrapper<QaAnswer> qw = new QueryWrapper<>();
        qw.eq("question_id",questionId);
        qw.orderByDesc("gmt_create");
        return (Page) baseMapper.selectPage(pageParam, qw);
    }
}
